import 'package:date_format/date_format.dart';
import 'package:division/division.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/custom_list_cell.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/ui/widget/unlogin_nav.dart';
import 'package:s_mankey/view_model/my_page_vmodel.dart';

class Waller extends StatefulWidget {
  final arguments;

  Waller({ this.arguments});

  @override
  _Waller createState() => _Waller();
}

class _Waller extends State<Waller> with TickerProviderStateMixin{
  Map<String,dynamic> datas;
  Map<String,dynamic> expDatas;


  TabController _tabController;

  var list = ['盛点', '经验'];
  String expTitle = '我的经验';
  int curIndex = 0; //0是盛点, 1是经验
  int startRow = 0;
  int endRow = 10;
  bool dataOnReady = false;

  @override
  void initState() {
    UnLoginNav(context).unLogin();
    _tabController = TabController(initialIndex: curIndex, length: list.length, vsync: this);
//    loadData(index: curIndex);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('学习里程'),
        elevation: 0,
      ),
      body: ProviderWidget<WalletVModel>(
        model: WalletVModel(),
        onModelReady: (model) => model.initData(),
        builder: (context, model ,child){
          print('类型是${model.expDatas}');
          return (model.expDatas == null)? LoadingWidget() :
          ListView(
            children: [
              Column(
                children: [
                  Container(
                    height: 200,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Padding(
                          padding: const EdgeInsets.fromLTRB(0, 10, 0, 30),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [

                              DefaultTabController(length: list.length, child: Container(color: Colors.white,child:  TabBar(
                                  controller: _tabController,
                                  indicatorColor: Color(0xffff3f7f),
                                  indicatorSize: TabBarIndicatorSize.label,
                                  indicatorWeight: 2,
                                  labelStyle: TextStyle(fontSize: AppFontSize.Size16, color: AppColor.lightGray),
                                  unselectedLabelStyle: TextStyle(fontSize: AppFontSize.Size16, color: AppColor.lightGray),
                                  isScrollable: true,
                                  onTap: (index){
                                    setState(() {
                                      curIndex = index;
                                    });
                                    print(list[index]);
                                  },
                                  tabs: List.generate(
                                    list.length,
                                        (index) =>  Tab(
                                      text: list[index],
                                    ),)),)),

                            ],
                          ),
                        ),
                        Offstage(
                          offstage: curIndex == 0,
                          child: Padding(
                            padding: const EdgeInsets.only(bottom: 20),
                            child:
                            Text(model.expDatas['exp_level_desc']),
                          ),
                        ),
                        Offstage(
                          offstage: curIndex != 0,
                          child: Padding(
                            padding: const EdgeInsets.only(bottom: 20),
                            child: Text('总盛点'),
                          ),
                        ),
                        Container(
                          width: 100,
                          padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              gradient: AppColor.gradientColorLight,
                              borderRadius: BorderRadius.circular(30)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(right: 5),
                                child: Icon(
                                  Icons.ac_unit,
                                  size: AppIconSize.Size16,
                                  color: Colors.white,
                                ),
                              ),
                              Text(
                                (curIndex == 0)? model.walletDatas['miles_total'].toString() : model.expDatas['exp_total'].toString()
                                ,
                                style: TextStyle(
                                    fontSize: AppFontSize.Size18,
                                    color: Colors.white),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(10),
                    width: AppCustomSize.ScreentWidght,
                    child: Text(
                      (curIndex == 0)? '盛点记录' : '经验值记录',
                      style: TextStyle(fontSize: AppFontSize.Size16),
                    ),
                    decoration: BoxDecoration(
                        border: Border(
                            top: BorderSide(color: Color(0xfff0f0f0)),
                            bottom: BorderSide(color: Color(0xfff0f0f0)))),
                  ),

                  (curIndex == 0) ?
                  /// 盛点的列表
                  ListView.builder(
                    primary: true,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: model.walletDatas['miles_breakdown'].length,
                    itemBuilder: (BuildContext context, int index) {
                      var _datas = model.walletDatas['miles_breakdown'][index];
                      String title = '';
                      int type = _datas['type'];
                      if (type == 1) title = '毕业课程';
                      if (type == 2) title = '成为組长';
                      if (type == 3) title = '成为学长';
                      if (type == 4) title = '盛点点';
                      if (type == 5) title = '签到';
                      if (type == 6) title = '成为总教导官';
                      return CustomListViewCell(
                        index: index,
//                leading: iconItems[index],
                        title: title,
                        subTitle: _datas['apply_date'].toString(),
                        trailing: Text(
                          ///TODO: 这里数值的状态还没有判断
                          _datas['value'].toString(),
                          style: TextStyle(
                              color: Color(0xffd65b5b),
                              fontSize: AppFontSize.Size12),
                        ),
                      );
                    },
                  ):

                  /// 经验的列表
                  ListView.builder(
                    primary: true,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: model.expDatas['exp_breakdown'].length,
                    itemBuilder: (BuildContext context, int index) {
                      var _datas = model.expDatas['exp_breakdown'][index];
                      return CustomListViewCell(
                        index: index,
//                leading: iconItems[index],
                        title: _datas['course_code'],
                        subTitle: _datas['apply_date'],
                        trailing: Text(
                          _datas['value'],
                          style: TextStyle(
                              color: Color(0xffd65b5b),
                              fontSize: AppFontSize.Size12),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ],
          );
            
        },
      ),


    );
  }

  Widget tabWidget ({String title, bool selIndex}){
    return GestureDetector(
      onTap: () {
        selIndex = !selIndex;
        setState(() {});
      },
      child: Column(
        children: [
          Text(
            title,
            style: TextStyle(
                color: selIndex
                    ? Color(0xffff48c4)
                    : Color(0xff909090)),
          ),
          Offstage(
            offstage: !selIndex,
            child: Container(
              child: Text(title),
              height: 1,
              decoration: BoxDecoration(
                  gradient: AppColor.gradientColor),
            ),
          ),
        ],
      ),
    );
  }
}
